package com.sp.system.controller;

import com.sp.common.core.controller.BaseController;
import com.sp.common.core.domain.AjaxResult;
import com.sp.common.core.redis.RedisCache;
import com.sp.system.service.ISysVehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 价格Controller
 *
 *
 * @date 2024-11-24
 */
@RestController
@RequestMapping("/system/amount")
public class SysAmountController extends BaseController implements ApplicationRunner {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    // 默认收费标准
    private static final double DEFAULT_5H = 5.0;
    private static final double DEFAULT_1D = 4.0;
    private static final double DEFAULT_3D = 3.0;
    private static final double DEFAULT_7D = 2.0;
    private static final double DEFAULT_OVER_7D = 1.0;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 在项目启动时判断redis是否有值，没有则创建默认值
        if ( !redisTemplate.hasKey("charge_5h") ) {
            redisTemplate.opsForValue().set("charge_5h", String.valueOf(DEFAULT_5H));
        }
        if ( !redisTemplate.hasKey("charge_1d") ) {
            redisTemplate.opsForValue().set("charge_1d", String.valueOf(DEFAULT_1D));
        }
        if ( !redisTemplate.hasKey("charge_3d") ) {
            redisTemplate.opsForValue().set("charge_3d", String.valueOf(DEFAULT_3D));
        }
        if ( !redisTemplate.hasKey("charge_7d") ) {
            redisTemplate.opsForValue().set("charge_7d", String.valueOf(DEFAULT_7D));
        }
        if ( !redisTemplate.hasKey("charge_over_7d") ) {
            redisTemplate.opsForValue().set("charge_over_7d", String.valueOf(DEFAULT_OVER_7D));
        }
    }

    /**
     * 根据小时数获取对应的收费标准
     */
    @GetMapping("/getByHours/{hours}")
    public AjaxResult getChargeByHours(@PathVariable("hours") int hours) {
        String duration = determineDurationByHours(hours);
        String key = getRedisKeyForDuration(duration);
        String charge = redisTemplate.opsForValue().get(key);
        if ( charge != null ) {
            return AjaxResult.success(new BigDecimal(charge));
        }
        return AjaxResult.error("收费标准不存在");
    }

    /**
     * 根据小时数判断对应的时长
     */
    private String determineDurationByHours(int hours) {
        if ( hours <= 5 ) {
            return "5h";  // 5小时及以下
        } else if ( hours <= 24 ) {
            return "1d";  // 1天及以下
        } else if ( hours <= 72 ) {
            return "3d";  // 3天及以下
        } else if ( hours <= 168 ) {
            return "7d";  // 7天及以下
        } else {
            return "over7d";  // 超过7天
        }
    }

    /**
     * 获取停车收费标准
     */
    @GetMapping("/get/{duration}")
    public AjaxResult getCharge(@PathVariable("duration") String duration) {
        String key = getRedisKeyForDuration(duration);
        String charge = redisTemplate.opsForValue().get(key);
        if ( charge != null ) {
            return AjaxResult.success(new BigDecimal(charge));
        }
        return AjaxResult.error("收费标准不存在");
    }

    /**
     * 设置停车收费标准
     */
    @PostMapping("/set/{duration}")
    @PreAuthorize("@ss.hasPermi('system:amount:set')")
    public AjaxResult setCharge(@PathVariable("duration") String duration, @RequestParam("amount") double amount) {
        String key = getRedisKeyForDuration(duration);
        redisTemplate.opsForValue().set(key, String.valueOf(amount));
        return AjaxResult.success("收费标准设置成功");
    }

    /**
     * 根据时长获取对应的Redis key
     */
    private String getRedisKeyForDuration(String duration) {
        switch (duration) {
            case "5h":
                return "charge_5h";
            case "1d":
                return "charge_1d";
            case "3d":
                return "charge_3d";
            case "7d":
                return "charge_7d";
            case "over7d":
                return "charge_over_7d";
            default:
                throw new IllegalArgumentException("未知时长：" + duration);
        }
    }
}
